package com.gxc.facotory;

import com.gxc.chain.UserParamFilterChain;
import com.gxc.filter.*;
import sun.security.jca.GetInstance;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author GongXincheng
 * @date 2021/8/16 14:50
 */
public class UserParamFilterChainFactory {

    private static final UserParamFilterChainFactory INSTANCE = new UserParamFilterChainFactory();

    private static final List<Class<? extends UserParamFilter>> FILTER_LIST;

    static {
        FILTER_LIST = new ArrayList<>();
        FILTER_LIST.add(AgeFilter.class);
        FILTER_LIST.add(BirthdayFilter.class);
        FILTER_LIST.add(SalaryFilter.class);
        FILTER_LIST.add(UserIdFilter.class);
        FILTER_LIST.add(UserNameFilter.class);
    }

    private UserParamFilterChainFactory() {
    }

    public static UserParamFilterChainFactory getInstance() {
        return INSTANCE;
    }


    public UserParamFilterChain getFilterChain() {
        return new UserParamFilterChain(this.getFilterChainInstance());
    }

    private List<UserParamFilter> getFilterChainInstance() {
        return FILTER_LIST.stream()
                .map(clazz -> {
                    try {
                        return clazz.newInstance();
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }


}
